/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using DaveSexton.DocProject.Resources;

namespace DaveSexton.DocProject
{
	[global::System.Serializable]
	public class BuildProcessComponentException : Exception
	{
		#region Public Properties
		public BuildProcessComponent Component { get { return component; } }

		public string ComponentInfo
		{
			get
			{
				return componentInfo ?? ((component != null) ? component.ToString() : null);
			}
		}
		#endregion

		#region Private / Protected
		private readonly BuildProcessComponent component;
		private readonly string componentInfo;
		#endregion

		#region Constructors
		public BuildProcessComponentException() { }
		public BuildProcessComponentException(string message) : base(message) { }
		public BuildProcessComponentException(string message, Exception inner) : base(message, inner) { }

		public BuildProcessComponentException(string message, BuildProcessComponent component)
			: base(message)
		{
			this.component = component;
		}

		public BuildProcessComponentException(string message, BuildProcessComponent component, Exception inner)
			: base(message, inner)
		{
			this.component = component;
		}

		public BuildProcessComponentException(BuildProcessComponent component)
			: base(GetErrorMessage(component, null))
		{
			this.component = component;
		}

		public BuildProcessComponentException(BuildProcessComponent component, Exception inner)
			: base(GetErrorMessage(component, inner), inner)
		{
			this.component = component;
		}

		protected BuildProcessComponentException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
			: base(info, context)
		{
			if (info == null)
				throw new ArgumentNullException("info");

			componentInfo = info.GetString("componentInfo");
		}
		#endregion

		#region Methods
		private static string GetErrorMessage(BuildProcessComponent component, Exception innerException)
		{
			return string.Format(System.Globalization.CultureInfo.CurrentCulture,
				Errors.BuildProcessComponentExceptionMessage, Environment.NewLine, component.ToString(),
				(innerException == null) ? string.Empty : innerException.GetType().FullName,
				(innerException == null) ? string.Empty : innerException.Message);
		}

		[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.LinkDemand, SerializationFormatter = true)]
		[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand, SerializationFormatter = true)]
		public override void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
		{
			if (info == null)
				throw new ArgumentNullException("info");

			info.AddValue("componentInfo", (component != null) ? component.ToString() : string.Empty);

			base.GetObjectData(info, context);
		}
		#endregion
	}
}
